import { Controller, Inject, Get, Query, Body, Post, Put, Param, Del } from '@midwayjs/core';
import { ConfigService } from "@service/system/config.service";
import { ListConfigDTO, CreateConfigDTO, UpdateConfigDTO } from "@dto/system/config.dto";
import { Auth } from "@decorator/auth.decorator";
import { BusinessType, Log } from "@decorator/log.decorator";

@Controller('/system/config')
export class PostController {
  @Inject()
  configService: ConfigService;

  // 获取列表
  @Auth('system:config:list')
  @Get('/list')
  async list(@Query() queryParams: ListConfigDTO) {
    return await this.configService.list(queryParams);
  }

  // 新增
  @Auth('system:config:add')
  @Log({ title: '参数配置', businessType: BusinessType.ADD })
  @Post('/')
  async create(@Body() config: CreateConfigDTO) {
    return await this.configService.create(config);
  }

  // 删除
  @Auth('system:config:remove')
  @Log({ title: '参数配置', businessType: BusinessType.REMOVE })
  @Del('/:configId')
  async delete(@Param('configId') configId: string) {
    return await this.configService.delete(configId);
  }

  // 修改
  @Auth('system:config:edit')
  @Log({ title: '参数配置', businessType: BusinessType.EDIT })
  @Put('/')
  async update(@Body() config: UpdateConfigDTO) {
    return await this.configService.update(config);
  }

  // 获取详情
  @Auth('system:config:query')
  @Get('/:configId')
  async get(@Param('configId') configId: number) {
    return await this.configService.detail(configId);
  }

  // 导出
  @Auth('system:config:export')
  @Log({ title: '参数配置', businessType: BusinessType.EXPORT })
  @Post('/export')
  async export(@Query() queryParams: ListConfigDTO) {
    return await this.configService.export(queryParams);
  }

  // 根据参数键名查询参数值
  @Get('/configKey/:configKey')
  async getConfigKey(@Param('configKey') configKey: string) {
    return await this.configService.getConfigKey(configKey);
  }

  // 刷新缓存
  @Del('/refreshCache')
  async refreshCache() {
    return await this.configService.refreshCache();
  }
}
